//
//  LGFImageTitleView.swift
//  MedicalUnionHybrid
//
//  Created by 来 on 2019/9/19.
//  Copyright © 2019 EWell. All rights reserved.
//

import UIKit

protocol LGFImageTitleViewDelegate: class {
    func selectLGFImageTitleView(_ view: LGFImageTitleView)
}

class LGFImageTitleView: UIView {
    
    weak var delegate: LGFImageTitleViewDelegate?
    
    var isSelect = false {
        didSet {
            if isSelect {
                label.textColor = scolor
                imageView.image = simage
            } else {
                label.textColor = UIColor.init(lgf_HexString: "666666")
                imageView.image = image
            }
        }
    }
    
    var imageView: UIImageView!
    var label: UILabel!
    
    var image: UIImage! {
        didSet {
            if imageView != nil {
                imageView.image = image
            }
        }
    }
    var simage: UIImage!
    var text: String! {
        didSet {
            if label != nil {
                label.text = text
                self.lgf_ViewName = text
            }
        }
    }
    var scolor: UIColor!
    var imageSize: CGSize!
    var bottomSp: CGFloat!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init(_ image: UIImage, _ simage: UIImage, _ scolor: String, _ text: String, _ imageSize: CGSize, _ bottomSp: CGFloat) {
        super.init(frame: .zero)
        self.lgf_ViewName = text
        self.image = image
        self.simage = simage
        self.text = text
        self.scolor = UIColor.init(lgf_HexString: scolor)
        self.imageSize = imageSize
        self.bottomSp = bottomSp
        configUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUI() {

        label = UILabel.init()
        label.text = self.text
        label.textColor = UIColor.init(lgf_HexString: "666666")
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 11)
        addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-bottomSp)
            make.height.equalTo(10)
        }
        
        imageView = UIImageView.init()
        imageView.image = self.image
        addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-5)
            make.width.equalTo(imageSize.width)
            make.height.equalTo(imageSize.height)
        }
        self.lgf_AddTap(target: self, action: #selector(selectView(_:)))
    }
    
    @objc func selectView(_ sender: UITapGestureRecognizer) -> Void {
        self.delegate?.selectLGFImageTitleView(self)
    }
}
